package com.cooper.dao.item;

import java.util.Collection;
import java.util.List;

import net.paoding.rose.jade.annotation.DAO;
import net.paoding.rose.jade.annotation.Insert;
import net.paoding.rose.jade.annotation.ReturnGeneratedKeys;
import net.paoding.rose.jade.annotation.SQL;
import net.paoding.rose.jade.annotation.Update;

import com.cooper.entity.ItemImage;

@DAO
public interface ItemImageDAO {
	
	static final String TABLE_NAME = "d_item_image";
	
	@ReturnGeneratedKeys
	@Insert(tableName=TABLE_NAME)
	Long saveEntity(ItemImage itemImage) throws Exception;
	
	@Update(tableName=TABLE_NAME, keyColumn="id")
	boolean updateEntity(ItemImage itemImage) throws Exception;
	
	@Insert(tableName=TABLE_NAME)
	void saveEntities(List<ItemImage> itemImageList) throws Exception;
	
	@SQL("update d_item_image set deleted='y' where item_id=:1 and id not in (:2) ")
	void deleteImagesNotUsedByItemId(long itemId, Collection<Long> imageIdList);
	
	@SQL("select * from d_item_image t where t.deleted='n' and t.id=:1 ")
	ItemImage loadById(long id) throws Exception;
	
	@SQL("select * from d_item_image t where t.deleted='n' and t.item_id=:1 order by t.sort_no asc")
	List<ItemImage> getItemImageListByItemId(long itemId) throws Exception;
	
	@SQL("update d_item_image set main_url=:1.mainUrl,gallery_url=:1.galleryUrl,thumb_url=:1.thumbUrl "
			+ "where deleted='n' and item_id=:1.itemId and sort_no=:1.sortNo ")
	int updateItemImageThumb(ItemImage itemImage);
}
